package com.mrzhou.core.tool.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.lang.Nullable;

import java.beans.PropertyDescriptor;
import java.util.*;
import java.util.stream.Collectors;

public class BeanUtil extends BeanUtils {

    public static <T> T copy(@Nullable Object source, Class<T> target) {
        try {
            T t = target.newInstance();
            copyProperties(source, t);
            return t;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 忽略拷贝目标对象中为空的数据
     */
    public static void copyPropertiesIgnoreTargetNotNull(Object source,Object target) {
        copyProperties(source, target, getPropertyNames(false, target));
    }

    /**
     * @param flag true返回source为空的字段, false返回source不为空的字段
     * @return
     */
    public static String[] getPropertyNames(final boolean flag, Object source) {
        final BeanWrapper wrapper = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = wrapper.getPropertyDescriptors();
        Set<String> propertyNames = new HashSet<>();
        for (PropertyDescriptor pd: pds) {
            Object value = wrapper.getPropertyValue(pd.getName());
            if(flag && value == null) {
                propertyNames.add(pd.getName());
            } else if (!flag && value != null) {
                propertyNames.add(pd.getName());
            }
        }
        String[] result = new String[propertyNames.size()];
        return propertyNames.toArray(result);
    }

    /**
     * 转换对象
     * @param source 源对象
     * @param target 目标对象
     * @param <T>
     * @return
     */
    public static <T> T covertObject(Object source, Class<T> target) {
        return JacksonUtil.parseObject(JacksonUtil.toJsonString(source), target);
    }

    public static <T> T covertObject(Object source, TypeReference<T> target) {
        return JacksonUtil.parseObject(JacksonUtil.toJsonString(source), target);
    }

    /**
     * 将对象转成map
     * @param obj
     * @return
     */
    public static Map<String, Object> beanToMap(Object obj) {
        Map<String, Object> map = new HashMap<>(4);
        if (null != obj) {
            TypeReference<Map<String, Object>> type = new TypeReference<Map<String, Object>>(){ };
            return JacksonUtil.parseObject(JacksonUtil.toJsonString(obj), type);
        }
        return map;
    }

    /**
     * 将集合转换指定的对象集合
     * @param list
     * @param target
     * @param <T>
     * @return
     */
    public static <T> List<T> converList(List<?> list, Class<T> target) {
        if (Func.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(var -> copy(var, target)).collect(Collectors.toList());
    }

}
